#!/bin/bash
CLUSTER_CONFIG="/data/nodes.conf"
hostname=$(hostname)
if [ -f ${CLUSTER_CONFIG} ]; then
    echo "[ INFO ]File:${CLUSTER_CONFIG} is Found"
else
    touch $CLUSTER_CONFIG
fi

if [ "${hostname}" == "redis-cluster-0" ]; then
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.50/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.50
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
elif [ "${hostname}" == "redis-cluster-1" ]; then
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.51/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.51
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
elif [ "${hostname}" == "redis-cluster-2" ]; then
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.52/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.52
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
elif [ "${hostname}" == "redis-cluster-3" ]; then
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.53/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.53
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
elif [ "${hostname}" == "redis-cluster-4" ]; then
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.54/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.54
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
else
    sed -i.bak -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/10.200.1.55/" ${CLUSTER_CONFIG}
    cat >> /etc/redis/redis.conf <<EOF
cluster-announce-ip 10.200.1.55
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
fi

echo "done"
exec "$@"
